﻿Imports System.IO
Imports System.ServiceModel
Imports System.ServiceModel.Activation

' HINWEIS: Mit dem Befehl "Umbenennen" im Kontextmenü können Sie den Klassennamen "MandeBackYardService" sowohl im Code als auch in der SVC-Datei und der Konfigurationsdatei ändern.
' HINWEIS: Wählen Sie zum Starten des WCF-Testclients zum Testen dieses Diensts MandeBackYardService.svc oder MandeBackYardService.svc.vb im Projektmappen-Explorer aus, und starten Sie das Debuggen.
<ServiceBehavior>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class MandeBackYardService
    Implements IBackYardService

    Public Function DownloadFile(request As DownloadRequest) As RemoteFileInfo Implements IBackYardService.DownloadFile
        CurrentOperationStatus.OperationsType = "downloadfile"
        Dim result As New RemoteFileInfo()
        Try
            Dim filePath As String = System.IO.Path.Combine(ManPath, request.FileName)
            Dim fileInfo As New System.IO.FileInfo(filePath)

            ' check if exists
            If Not fileInfo.Exists Then
                result.FileName = "FileNotFoundException"
                result.Length = -1
                result.FileByteStream = Nothing
                CurrentOperationStatus.HasError = True
                CurrentOperationStatus.Errors = New Exception("FileCurrentlyNotFoundOnServer..")
            Else
                Try
                    Dim stream As New System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)

                    ' return result 
                    result.FileName = request.FileName
                    result.Length = fileInfo.Length
                    result.FileByteStream = stream
                    CurrentOperationStatus.HasError = False
                    CurrentOperationStatus.Errors = Nothing
                Catch ex As Exception
                    CurrentOperationStatus.HasError = True
                    CurrentOperationStatus.Errors = ex
                End Try


            End If

            ' open stream

        Catch ex As Exception
            CurrentOperationStatus.HasError = True
            CurrentOperationStatus.Errors = ex
        End Try
        Return result
    End Function

    Public Sub UploadFile(request As RemoteFileInfo) Implements IBackYardService.UploadFile
        ' Dim targetStream As FileStream = Nothing
        CurrentOperationStatus.OperationsType = "uploadfile"
        Try
            Dim sourceStream As Stream = request.FileByteStream
            Dim filePath As String
            If request.OnDelete = True Then
                filePath = Path.Combine(ManPath, request.FileName)
                System.IO.File.Delete(filePath)
            End If


            filePath = Path.Combine(ManPath, request.FileName)

            Using targetStream = New FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.None)
                request.FileByteStream.CopyTo(targetStream)
            End Using
            CurrentOperationStatus.Errors = Nothing
            CurrentOperationStatus.HasError = False
        Catch ex As Exception
            CurrentOperationStatus.Errors = ex
            CurrentOperationStatus.HasError = True
        End Try
    End Sub

    Public Function GetOperationStatus() As OperationStatus Implements IBackYardService.GetOperationStatus
        Return CurrentOperationStatus
    End Function

    Public Function DeleteBackYardFile(filename As String) As Boolean Implements IBackYardService.DeleteBackYardFile
        If System.IO.File.Exists(System.IO.Path.Combine(ManPath, filename)) Then
            Try
                Dim fileinfo As New FileInfo(System.IO.Path.Combine(ManPath, filename))
                fileinfo.Delete()
                Return True
            Catch
                Return False
            End Try
        Else
            Return False
        End If
    End Function

    Public Function EnumerateFiles() As List(Of FileInfo) Implements IBackYardService.EnumerateFiles
        Dim retlist As New List(Of FileInfo)
        Dim dirinfo As New System.IO.DirectoryInfo(ManPath)
        For Each a In dirinfo.GetFiles
            retlist.Add(a)
        Next
        Return retlist
    End Function
End Class
